package Interface;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Sess
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import Main.*;
import Process.*;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;

/**
 *
 * @author Sess
 */
public class JoinGame extends JFrame implements ActionListener {

    public Hearts control;
    JTextField jTextFieldIP, jTextFieldHostName;
    JPasswordField jPasswordFieldPassword;

    public JoinGame(Hearts control) {
        this.control = control;
        try {
            // Set cross-platform Java L&F (also called "Metal")
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (UnsupportedLookAndFeelException e) {
            // handle exception
        } catch (ClassNotFoundException e) {
            // handle exception
        } catch (InstantiationException e) {
            // handle exception
        } catch (IllegalAccessException e) {
            // handle exception
        }
        setTitle("Join Game");
        setResizable(false);
        setSize(300, 150);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


        getContentPane().setLayout(new GridBagLayout());

        JLabel jLabelIP = new JLabel("IP Server:");
        AddComponent(0, 0, 1, 1, jLabelIP);

        jTextFieldIP = new JTextField();
        AddComponent(0, 1, 1, 1, jTextFieldIP);

        jTextFieldIP.setText("localhost");

        JButton jButtonJoin = new JButton("Join Game");
        jButtonJoin.setActionCommand("Join");

        jButtonJoin.addActionListener(this);
        AddComponent(2, 0, 1, 1, jButtonJoin);

        JButton jButtonCancel = new JButton("Cancel");
        jButtonCancel.setActionCommand("Cancel");

        jButtonCancel.addActionListener(this);
        AddComponent(2, 1, 1, 1, jButtonCancel);


    }

    public void actionPerformed(ActionEvent e) {

        String strActionCommand = e.getActionCommand();
        if (strActionCommand.equals("Join")) {


            control.IP = jTextFieldIP.getText();
            if (control.IP.equals("")) {
                return;
            }
          

                control.ShowClientInterfaceFrame();
                //control.frameClientInterface.setVisible(true);
        


        }

        if (strActionCommand.equals("Cancel")) {
            control.frameJoinGame.setVisible(false);
            control.frameMainMenu.setVisible(true);
        }

    }

    private void AddComponent(int row, int column, int Gridwidth, int GridHeight, JComponent comp) {
        GridBagConstraints gbc;
        gbc = new GridBagConstraints();
        gbc.gridx = column;
        gbc.gridy = row;
        gbc.gridwidth = Gridwidth;
        gbc.gridheight = GridHeight;
        gbc.weightx = 1.0;
        gbc.weighty = 0.0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.insets.set(5, 5, 5, 5);
        getContentPane().add(comp, gbc);
    }
}
